package com.jh.readme.iu.fragment.news;

import android.support.v4.view.ViewPager;
import android.view.View;
import android.support.design.widget.TabLayout;

import com.jh.readme.R;
import com.jh.readme.iu.fragment.news.common.MyTabItem;
import com.jh.readme.iu.fragment.news.common.SimpleFragmentPagerAdapter;
import com.jh.readme.iu.fragment.news.entity.NewsEntity;
import com.jh.tools.api.Api;
import com.jh.tools.base.BaseFragment;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 新闻列表页
 */
public class NewsFragmente extends BaseFragment<NewsPresenter,NewsModel> implements NewsContract.View {

    private static final String POSITION = "position";
    private SimpleFragmentPagerAdapter pagerAdapter;

   public static ViewPager news_content;

    public static TabLayout tab_news_title;

    MyTabItem[] tabs;
    @Override
    public int getLayoutId() {
        return R.layout.fragment_news;
    }
    private List<NewsEntity> mNewsEntitys;
    @Override
    public void initView(View baseView) {
        tab_news_title=(TabLayout) baseView.findViewById(R.id.tab_news_title);
        news_content=(ViewPager) baseView.findViewById(R.id.news_content);
        mNewsEntitys=new ArrayList<>();
        List<String> channelName = Arrays.asList(this.getResources()
                .getStringArray(R.array.news_channel));

        List<String> channelId = Arrays.asList(this.getResources()
                .getStringArray(R.array.news_channel_id));
        for (int i = 0; i <2 ; i++) {
            NewsEntity newsEntity=new NewsEntity();
            newsEntity.tabNewsName=channelName.get(i);
            newsEntity.newsId= channelId.get(i);
            newsEntity.newsType=Api.getType(channelId.get(i));
            mNewsEntitys.add(newsEntity);
        }
        initTab(mNewsEntitys);
    }

    @Override
    public void initPresenter() {
     mPresenter.setVM(this,mModel,this);
    }


    @Override
    public void initTab(List<NewsEntity> mNewsEntitys) {
        pagerAdapter = new SimpleFragmentPagerAdapter(this.getFragmentManager(),mNewsEntitys, this.getActivity());
        news_content.setAdapter(pagerAdapter);

        tab_news_title.setupWithViewPager(news_content);

        tabs = new MyTabItem[tab_news_title.getTabCount()];
        int size=tab_news_title.getTabCount();
        for (int i = 0; i < size; i++) {
            TabLayout.Tab tab = tab_news_title.getTabAt(i);
            tabs[i] = new MyTabItem(this.getContext(),mNewsEntitys.get(i).tabNewsName);
            tab.setCustomView(tabs[i].getTabView());
//            tab_news_title.addTab(tab_news_title.newTab().setText(mNewsEntitys.get(i).tabNewsName));
        }

        news_content.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                tabs[position].setIndicVisibility(3);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }
}
